package base.loader {
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.events.IOErrorEvent;	
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.system.ApplicationDomain;
	import flash.system.SecurityDomain;
	
	public class LoaderExt extends Loader {
		
		public function LoaderExt(loaderPath: String, onLoadCompleteHandler: Function = null, onLoadProgressHandler: Function = null, onLoadErrorHandler: Function = null, checkPolicyFile: Boolean = false) {
			super();
			if (onLoadCompleteHandler == null) onLoadCompleteHandler = this.onLoadCompleteHandler;
			if (onLoadProgressHandler == null)	onLoadProgressHandler = this.onLoadProgressHandler;
			if (onLoadErrorHandler == null)	onLoadErrorHandler = this.onLoadErrorHandler;
            this.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadCompleteHandler);
			this.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoadProgressHandler);
            this.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadErrorHandler);
			var request:URLRequest = new URLRequest(loaderPath);
			var context:LoaderContext = new LoaderContext(checkPolicyFile, ApplicationDomain.currentDomain); //SecurityDomain.currentDomain
            this.load(request, context);
		}
			
		private function onLoadCompleteHandler(event:Event): void {
			trace("The img / movie has finished loading.");
		}
		
		private function onLoadProgressHandler(event:ProgressEvent): void {
			var ratioLoaded:Number = event.bytesLoaded / event.bytesTotal;
			var percentLoaded: uint = Math.round(ratioLoaded * 100);
			//trace("The img / movie has loaded in " + percentLoaded + " %");
		}
		
		private function onLoadErrorHandler(errorEvent:IOErrorEvent): void {
            trace("The img / movie could not be loaded: " + errorEvent.text);
        }
		
	}
	
}